#include "settingdialog.h"
#include "ui_settingdialog.h"

SettingDialog::SettingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingDialog)
{
    ui->setupUi(this);
    iniMainDialog();

    connect(this,&SettingDialog::sigSetDarkTheme,
            AppEvent::getInstance(),&AppEvent::onSetDarkTheme);
    connect(this,&SettingDialog::sigSetLightTheme,
            AppEvent::getInstance(),&AppEvent::onSetLightTheme);
    connect(btnOK,&QPushButton::clicked,
            this,&SettingDialog::on_btnOK_clicked);
    connect(btnApply,&QPushButton::clicked,
            this,&SettingDialog::on_btnApply_clicked);
    connect(btnCancel,&QPushButton::clicked,
            this,&SettingDialog::on_btnCancel_clicked);
}

SettingDialog::~SettingDialog()
{
    delete ui;
}
void SettingDialog::iniMainDialog()
{
    comboxTheme=new QComboBox();
    comboxTheme->addItem(QObject::tr("Dark"));
    comboxTheme->addItem(QObject::tr("Light"));
    comboxTheme->setCurrentIndex(1);
    comboxLanguage=new QComboBox();
    comboxLanguage->addItem(QObject::tr("English"));
    comboxLanguage->addItem(QObject::tr("Chinese"));
    btnOK=new QPushButton();
    btnOK->setText(QObject::tr("OK"));
    btnApply=new QPushButton();
    btnApply->setText(QObject::tr("Apply"));
    btnCancel=new QPushButton();
    btnCancel->setText(QObject::tr("Cancel"));
    QFormLayout *formlayoutInterface=new QFormLayout();
    formlayoutInterface->insertRow(0,QObject::tr("Theme"),comboxTheme);
    formlayoutInterface->insertRow(1,QObject::tr("Language"),comboxLanguage);
    QHBoxLayout *btnLayout=new QHBoxLayout();
    btnLayout->addWidget(btnOK);
    btnLayout->addWidget(btnCancel);
    btnLayout->addWidget(btnApply);
    QVBoxLayout *mainLayout=new QVBoxLayout();
    mainLayout->addLayout(formlayoutInterface);
    mainLayout->addLayout(btnLayout);
    this->setLayout(mainLayout);
}
//SLOTS
void SettingDialog::on_btnOK_clicked()
{
    if(comboxTheme->currentText()=="Dark"){
        emit sigSetDarkTheme();
    }
    if(comboxTheme->currentText()=="Light"){
        emit sigSetLightTheme();
    }
    this->close();
}
void SettingDialog::on_btnApply_clicked()
{
    this->close();
}
void SettingDialog::on_btnCancel_clicked()
{
    if(comboxTheme->currentText()=="Dark"){
        emit sigSetDarkTheme();
    }
    if(comboxTheme->currentText()=="Light"){
        emit sigSetLightTheme();
    }
}
